<template>
  <div class="p-2">

    <div class="mb-[10px]">
      <el-card>
        <el-form
          ref="queryFormRef"
          :model="queryParams"
          :inline="true"
          label-width="auto"
        >
          <el-form-item label="创建时间">
            <el-date-picker
              v-model="dateRange"
              value-format="YYYY-MM-DD"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              style="width: 240px"
            ></el-date-picker>
          </el-form-item>
          <el-form-item
            label="代理商名称"
            prop="name"
          >
            <el-input
              v-model="queryParams.name"
              placeholder="请输入代理商名称"
              clearable
              style="width: 240px"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item
            v-if="type=='product'"
            label="代理商编号"
            prop="no"
          >
            <el-input
              v-model="queryParams.no"
              placeholder="请输入代理商编号"
              clearable
              style="width: 240px"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item
            label="代理商级别"
            prop="dwLevel"
          >
            <el-select
              v-model="queryParams.dwLevel"
              placeholder="请输入代理商级别"
              clearable
              filterable
              style="width: 240px"
            >
              <el-option
                v-for="(item,i) in agent_level"
                :label="item.label"
                :value="item.value"
                :key="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item
            label="代理商类型"
            prop="dwType"
          >
            <el-select
              v-model="queryParams.dwType"
              placeholder="请选择代理商类型"
              clearable
              filterable
              style="width: 240px"
            >
              <el-option
                v-for="(item,i) in agent_type"
                :label="item.label"
                :value="item.value"
                :key="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item
            label="审核状态"
            prop="verifyStatus"
          >
            <el-select
              v-model="queryParams.verifyStatus"
              placeholder="请选择审核状态"
              clearable
              filterable
              style="width: 240px"
            >
              <el-option
                v-for="(item,i) in mer_check_status"
                :label="item.label"
                :value="item.value"
                :key="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              @click="handleQuery"
              icon="Search"
              v-hasPermi="['payment:channel:list']"
            >查询</el-button>
            <el-button
              @click="resetQuery"
              icon="Refresh"
            >重置</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
    <el-card>
      <template
        v-if="type!='product' && showAdd"
        #header
      >
        <el-row :gutter="10">
          <el-col :span="1.5">
            <el-button
              type="primary"
              @click="addAndEditAgent('add')"
              v-hasPermi="['payment:channel:add']"
              icon="Plus"
            >新增</el-button>
          </el-col>
        </el-row>
      </template>

      <el-table
        v-loading="loading"
        :data="agentList"
        border
      >
        <el-table-column
          label="创建时间"
          align="center"
          min-width="160"
          prop="createTime"
          sortable
        />
        <el-table-column
          label="代理商名称"
          align="center"
          min-width="160"
          prop="name"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="代理商编号"
          align="center"
          min-width="160"
          prop="no"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="代理商类型"
          align="center"
          min-width="160"
          prop="dwType"
        >
          <template #default="scope">
            <dict-tag
              :options="agent_type"
              :value="scope.row.dwType"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="代理商等级"
          align="center"
          min-width="160"
          prop="dwLevel"
          :show-overflow-tooltip="true"
        >
          <template #default="scope">
            <dict-tag
              :options="agent_level"
              :value="scope.row.dwLevel"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="审核状态"
          align="center"
          min-width="160"
          prop="verifyStatusName"
        />
        <el-table-column
          label="操作"
          fixed="right"
          align="center"
          width="180"
          class-name="small-padding fixed-width"
        >
          <template #default="scope">
            <el-tag
              v-if="type!='product' &&  scope.row.verifyStatusName != '审核通过'"
              link
              @click="addAndEditAgent('edit',scope.row)"
              v-hasPermi="['payment:channel:edit']"
            >编辑</el-tag>
            <el-tag
              v-if="type!='product'"
              link
              @click="addAndEditAgent('look',scope.row)"
              v-hasPermi="['payment:channel:query']"
            >详情</el-tag>
            <el-tag
              v-if="type!='product' &&  scope.row.verifyStatusName == '待审核' && roleId== 7 "
              link
              type="success"
              v-hasPermi="['payment:channel:check']"
              @click="checkAgent(scope.row)"
            >审核</el-tag>
            <el-tag
              v-if="type=='product'"
              link
              type="success"
               v-hasPermi="['payment:channelProduct:add']"
              @click="productConfig(scope.row)"
            >产品配置</el-tag>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <Check ref="agentCheckRef" :title="checkParams.title" :openCheck="checkParams.openCheck" :item="checkParams.item" :id="checkParams.id" @dialogClose="dialogClose" @dialogSubmit="dialogSubmit"></Check>

  </div>
</template>

<script setup name="agentTable" lang="ts">
import api from '@/api/agent/addAgent';
import { AgentQuery, AgentVO } from '@/api/agent/addAgent/types';
import useUserStore  from '@/store/modules/user'
import Check from "@/components/customDialog/check/index.vue"

const props = defineProps({
  type: {
    type: String,
    default: ''
  }
});

const agentCheckRef = ref();
const checkParams = ref({
  'title':'代理商审核',
  openCheck:false,
  item:{},
  id:''
});



const checkAgent = (row:any)=>{
  checkParams.value.openCheck = true;
  checkParams.value.item = row;
  checkParams.value.id = row.id;
}


const dialogClose = ()=>{
  checkParams.value.openCheck = false;
}


const dialogSubmit = (params:any)=>{
  let subParams = {
    chId:params.id,
    checkType:params.checkType,
    reason:params.remarer,
    domain:location.origin
  }
  api.checkChannel(subParams).then(res=>{
    agentCheckRef.value.hideLoad();
    dialogClose();
    getList();
  })
}



const roleId = ref();
roleId.value = useUserStore().curRoleId;

const router = useRouter();
const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const { agent_level, agent_type, mer_check_status } = toRefs<any>(proxy?.useDict('agent_type', 'agent_level', 'mer_check_status'));

const agentList = ref<AgentVO[]>();
const loading = ref(true);
const total = ref(0);
const dateRange = ref<[DateModelType, DateModelType]>(['', '']);
const queryFormRef = ref<ElFormInstance>();
const queryParams = ref<AgentQuery>({
  pageNum: 1,
  pageSize: 10,
  dwLevel: '',
  dwType: '',
  verifyStatus: '',
  name: '',
  no: '',
  isProduct:props.type ? 1:0
});

/** 查询代理商列表 */
const getList = async () => {
  loading.value = true;
  const res = await api.agentList(proxy?.addDateRange(queryParams.value, dateRange.value));
  loading.value = false;
  agentList.value = res.rows;
  total.value = res.total;
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  dateRange.value = ['', ''];
  queryFormRef.value?.resetFields();
  queryParams.value.pageNum = 1;
  handleQuery();
};

/** 新增&修改 */
const addAndEditAgent = (type: string, row: any = '') => {
  router.push(type == 'look' ? 'agentDetail?id=' + row.id : type == 'edit' ? 'agentEdit?id=' + row.id : 'agentAdd');
};
/** 产品配置 */
const productConfig = (row: any) => {
  router.push('agentProductConfig?id=' + row.id + '&no=' + row.no + '&name=' + row.name);
};

const showAdd = ref(true)

onMounted(() => {
  getList(); // 初始化列表数据
  let busNo = useUserStore().userInfo.busNo;
  if(busNo && busNo.includes('-D')){
    showAdd.value = false;
  }else{
    showAdd.value = true;
  }

});
</script>

<style lang="scss" scoped></style>
